package com.tyk.bookstore.front.member.controller;


import com.tyk.bookstore.front.common.core.model.result.R;
import com.tyk.bookstore.front.common.other.code.behavior.model.enume.SceneType;
import com.tyk.bookstore.front.member.model.query.*;
import com.tyk.bookstore.front.member.model.vo.MemberVo;
import com.tyk.bookstore.front.member.service.MemberCenterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "会员管理/个人中心", description = "会员管理")
@RequestMapping({"api/member/center"})
@RestController
@Validated
@RequiredArgsConstructor
public class MemberCenterController {

    private final MemberCenterService memberService;


    @PostMapping({"code/msg/phone/check-send"})
    @Operation(summary = "发送手机验证码(个人中心,含行为验证码校验)", description = "发送手机验证码(个人中心,含行为验证码校验)")
    public R<String> sendMsgCode4CenterWithBehaviorCheck(
            @Validated @RequestBody MsgCodeSendWithBehaviorCheckLoggedQuery query
    ) {
        return R.ok(memberService.sendMsgCodeWithBehaviorCheck(query, SceneType.CENTER));
    }

    @PostMapping({"code/msg/phone/check"})
    @Operation(summary = "校验手机验证码(个人中心)", description = "校验手机验证码(个人中心)")
    public R<String> checkMsgCode4Center(
            @Validated @RequestBody MsgCodeCheckLoggedQuery query
    ) {
        return R.ok(memberService.checkMsgCode(query, SceneType.CENTER));
    }

    @PostMapping({"code/msg/send"})
    @Operation(summary = "发送验证码(个人中心,新手机号/邮箱,不校验行为验证码)", description = "发送手机验证码(个人中心,新手机号/邮箱,不校验行为验证码)")
    public R<String> sendMsgCode4Center(
            @Validated @RequestBody MsgCodeSendNotRegisteredQuery query
    ) {
        return R.ok(memberService.sendMsgCodeNotRegistered(query, SceneType.CENTER));
    }


    @PutMapping({"phone"})
    @Operation(summary = "更新手机号", description = "更新手机号")
    public R<Object> updatePhone(@Validated @RequestBody PhoneUpdateQuery query) {
        memberService.updatePhone(query);
        return R.ok();
    }


    @PutMapping({"email"})
    @Operation(summary = "更新邮箱", description = "更新邮箱")
    public R<Object> updateEmail(@Validated @RequestBody EmailUpdateQuery query) {
        memberService.updateEmail(query);
        return R.ok();
    }

    @PutMapping({"password/login"})
    @Operation(summary = "更新登陆密码", description = "更新登陆密码")
    public R<Object> updateLoginPassword(@Validated @RequestBody LoginPasswordUpdateQuery query) {
        memberService.updateLoginPassword(query);
        return R.ok();
    }

    @PutMapping({"password/pay"})
    @Operation(summary = "更新支付密码", description = "更新支付密码")
    public R<Object> updatePayPassword(@Validated @RequestBody PayPasswordUpdateQuery query) {
        memberService.updatePayPassword(query);
        return R.ok();
    }

    @PutMapping({"id-card"})
    @Operation(summary = "更新身份证号", description = "更新身份证号")
    public R<Object> updateIdCard(@Validated @RequestBody IdCardUpdateQuery query) {
        memberService.updateIdCard(query);
        return R.ok();
    }

    @PutMapping({"avatar"})
    @Operation(summary = "更新会员头像", description = "更新会员头像")
    public R<String> updateAvatar(MultipartFile file) {
        return R.ok(memberService.updateAvatar(file));
    }


    @GetMapping
    @Operation(summary = "获取会员详情", description = "获取会员详情")
    public R<MemberVo> getMember() {
        return R.ok(memberService.getMember());
    }

    @PostMapping({"info"})
    @Operation(summary = "更新会员信息", description = "更新会员信息")
    public R<Object> updateMember(@Validated @RequestBody MemberUpdateQuery query) {
        memberService.updateMember(query);
        return R.ok();
    }
}
